package com.siwei.mes.entity.tgl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.siwei.mes.common.validator.bus.SyncGroup;
import com.siwei.mes.entity.Experiment;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;

import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.Date;


/**
 * TGL-小票(TglItemorder)实体类
 *
 * @author linzi
 * @since 2023-12-09 11:38:27
 */
@Data
@Accessors(chain = true)
@Table(name = "tgl_itemorder")
@ApiModel(value = "TglItemorder", description = "TGL-小票")
public class TglItemorder implements Serializable {

    /**
     * 主键id
     */
    @Id
    @ApiModelProperty(value = "TGL-小票主键ID", name = "id")
    private Long id;
    /**
     * 小票id
     */
    @Column(name = "ITID")
    @ApiModelProperty(value = "小票id", name = "itid")
    private Integer itid;
    /**
     * 小票编号
     */
    @Column(name = "itemOrderNo")
    @ApiModelProperty(value = "小票编号", name = "itemorderno")
    private String itemorderno;
    /**
     * 拌站id
     */
    @Column(name = "deliverCompanyId")
    @ApiModelProperty(value = "拌站id", name = "delivercompanyid")
    private Integer delivercompanyid;
    /**
     * 唯一键
     */
    @Column(name = "uniqueKey")
    @ApiModelProperty(value = "唯一键", name = "uniquekey")
    private String uniquekey;
    /**
     * 父小票id
     */
    @Column(name = "masterITID")
    @ApiModelProperty(value = "父小票id", name = "masteritid")
    private Integer masteritid;
    /**
     * 1-一车多票的主票
     */
    @Column(name = "hasSlave")
    @ApiModelProperty(value = "1-一车多票的主票", name = "hasslave")
    private Integer hasslave;
    /**
     * 任务单号
     */
    @Column(name = "FRwdh")
    @ApiModelProperty(value = "任务单号", name = "frwdh")
    private Integer frwdh;
    /**
     * 车号
     */
    @Column(name = "carNumber")
    @ApiModelProperty(value = "车号", name = "carnumber")
    private String carnumber;
    /**
     * 车辆id
     */
    @Column(name = "vehicleId")
    @ApiModelProperty(value = "车辆id", name = "vehicleid")
    private Integer vehicleid;
    /**
     * 司机名
     */
    @Column(name = "driverName")
    @ApiModelProperty(value = "司机名", name = "drivername")
    private String drivername;
    /**
     * 砼品种
     */
    @Column(name = "tpz")
    @ApiModelProperty(value = "砼品种", name = "tpz")
    private String tpz;
    /**
     * 塌落度
     */
    @Column(name = "tld")
    @ApiModelProperty(value = "塌落度", name = "tld")
    private String tld;
    /**
     * 浇筑部位
     */
    @Column(name = "jzbw")
    @ApiModelProperty(value = "浇筑部位", name = "jzbw")
    private String jzbw;
    /**
     * 浇筑方式
     */
    @Column(name = "jzfs")
    @ApiModelProperty(value = "浇筑方式", name = "jzfs")
    private String jzfs;
    /**
     * 工程名称
     */
    @Column(name = "projectName")
    @ApiModelProperty(value = "工程名称", name = "projectname")
    private String projectname;
    /**
     * 工程地址
     */
    @Column(name = "projectAddress")
    @ApiModelProperty(value = "工程地址", name = "projectaddress")
    private String projectaddress;
    /**
     * 备注
     */
    @Column(name = "remarks")
    @ApiModelProperty(value = "备注", name = "remarks")
    private String remarks;
    /**
     * 收货方名字
     */
    @Column(name = "receiveCompanyName")
    @ApiModelProperty(value = "收货方名字", name = "receivecompanyname")
    private String receivecompanyname;
    /**
     * 总发货数量
     */
    @Column(name = "fhQuantity")
    @ApiModelProperty(value = "总发货数量", name = "fhquantity")
    private Float fhquantity;
    /**
     * 混凝土生产方量
     */
    @Column(name = "scQuantity")
    @ApiModelProperty(value = "混凝土生产方量", name = "scquantity")
    private Float scquantity;
    /**
     * 砂浆生产方量
     */
    @Column(name = "sjscQuantity")
    @ApiModelProperty(value = "砂浆生产方量", name = "sjscquantity")
    private Float sjscquantity;
    /**
     * 生产总方量
     */
    @Column(name = "totalScQuantity")
    @ApiModelProperty(value = "生产总方量", name = "totalscquantity")
    private Float totalscquantity;
    /**
     * 剩余方量
     */
    @Column(name = "syQuantity")
    @ApiModelProperty(value = "剩余方量", name = "syquantity")
    private Float syquantity;
    /**
     * 签收方量
     */
    @Column(name = "qrQuantity")
    @ApiModelProperty(value = "签收方量", name = "qrquantity")
    private Float qrquantity;
    /**
     * 总盘数
     */
    @Column(name = "totalpan")
    @ApiModelProperty(value = "总盘数", name = "totalpan")
    private Integer totalpan;
    /**
     * 各盘信息 json
     */
    @Column(name = "paninfo")
    @ApiModelProperty(value = "各盘信息 json", name = "paninfo")
    private String paninfo;
    /**
     * 累计方量
     */
    @Column(name = "finishQuantity")
    @ApiModelProperty(value = "累计方量", name = "finishquantity")
    private Float finishquantity;
    /**
     * 累计车数
     */
    @Column(name = "finishCarNumber")
    @ApiModelProperty(value = "累计车数", name = "finishcarnumber")
    private Integer finishcarnumber;
    /**
     * 拌台号
     */
    @Column(name = "mixtable")
    @ApiModelProperty(value = "拌台号", name = "mixtable")
    private String mixtable;
    /**
     * 1-小票 2-水票
     */
    @Column(name = "itemtype")
    @ApiModelProperty(value = "1-小票 2-水票", name = "itemtype")
    private Integer itemtype;
    /**
     * 额外信息
     */
    @Column(name = "extrainfo")
    @ApiModelProperty(value = "额外信息", name = "extrainfo")
    private String extrainfo;
    /**
     * 出厂时间
     */
    @Column(name = "startTime")
    @ApiModelProperty(value = "出厂时间", name = "starttime")
    private Date starttime;
    /**
     * 到达时间
     */
    @Column(name = "arriveTime")
    @ApiModelProperty(value = "到达时间", name = "arrivetime")
    private Date arrivetime;
    /**
     * 离开时间
     */
    @Column(name = "leaveTime")
    @ApiModelProperty(value = "离开时间", name = "leavetime")
    private Date leavetime;
    /**
     * 签收时间
     */
    @Column(name = "signTime")
    @ApiModelProperty(value = "签收时间", name = "signtime")
    private Date signtime;
    /**
     * 回厂时间
     */
    @Column(name = "returnTime")
    @ApiModelProperty(value = "回厂时间", name = "returntime")
    private Date returntime;
    /**
     * 是否签收
     */
    @Column(name = "isSign")
    @ApiModelProperty(value = "是否签收", name = "issign")
    private Integer issign;
    /**
     * 0-作废 1-生产中(waiting) 2-运输中(finish) 3-已完成(complete)
     */
    @Column(name = "state")
    @ApiModelProperty(value = "0-作废 1-生产中(waiting) 2-运输中(finish) 3-已完成(complete)", name = "state")
    private Integer state;
    /**
     * 1- 生产中作废 2 - 生产完成作废
     */
    @Column(name = "canceltype")
    @ApiModelProperty(value = "1- 生产中作废 2 - 生产完成作废 ", name = "canceltype")
    private Integer canceltype;
    /**
     * tgl小票号
     */
    @Column(name = "jlbIds")
    @ApiModelProperty(value = "tgl小票号", name = "jlbids")
    private String jlbids;
    /**
     * 操作员
     */
    @Column(name = "czy")
    @ApiModelProperty(value = "操作员", name = "czy")
    private String czy;
    /**
     * 运距
     */
    @Column(name = "transportDistance")
    @ApiModelProperty(value = "运距", name = "transportdistance")
    private Float transportdistance;
    /**
     * 砼行id
     */
    @Column(name = "THID")
    @ApiModelProperty(value = "砼行id", name = "thid")
    private Integer thid;
    /**
     * 创建时间
     */
    @Column(name = "createTime")
    @ApiModelProperty(value = "创建时间", name = "createtime")
    private Date createtime;
    /**
     * 更新时间
     */
    @Column(name = "updateTime")
    @ApiModelProperty(value = "更新时间", name = "updatetime")
    private Date updatetime;
    /**
     * 打印次数
     */
    @Column(name = "printcount")
    @ApiModelProperty(value = "打印次数", name = "printcount")
    private Integer printcount;
    /**
     * 小票生产时的容重
     */
    @Column(name = "rz")
    @ApiModelProperty(value = "小票生产时的容重", name = "rz")
    private Float rz;
    /**
     * 出厂方量
     */
    @Column(name = "ccQuantity")
    @ApiModelProperty(value = "出厂方量", name = "ccquantity")
    private Float ccquantity;
    /**
     * 回厂方量
     */
    @Column(name = "hcQuantity")
    @ApiModelProperty(value = "回厂方量", name = "hcquantity")
    private Float hcquantity;
    /**
     * 累计出厂方量
     */
    @Column(name = "ccsumQuantity")
    @ApiModelProperty(value = "累计出厂方量", name = "ccsumquantity")
    private Float ccsumquantity;
    /**
     * 拌车称重打印 0：否 1：是
     */
    @Column(name = "isPrint")
    @ApiModelProperty(value = "拌车称重打印 0：否 1：是", name = "isprint")
    private Integer isprint;
    /**
     * 施工配比ID
     */
    @Column(name = "FSgpb")
    @ApiModelProperty(value = "施工配比ID", name = "fsgpb")
    private Integer fsgpb;
    /**
     * 砂浆配比ID
     */
    @Column(name = "FSjpb")
    @ApiModelProperty(value = "砂浆配比ID", name = "fsjpb")
    private Integer fsjpb;
    /**
     * 小票创建时的关联任务单配比原材料设定用量 json
     */
    @Column(name = "rwdphbycl")
    @ApiModelProperty(value = "小票创建时的关联任务单配比原材料设定用量 json", name = "rwdphbycl")
    private String rwdphbycl;
    /**
     * 同步时间戳
     */
    @Column(name = "synctime")
    @ApiModelProperty(value = "同步时间戳", name = "synctime")
    @NotNull(groups = {SyncGroup.class}, message = "同步时间戳为空")
    private Long synctime;

    /**
     * 车牌号
     */
    @Column(name = "carLicenseNo")
    @ApiModelProperty(value = "车牌号", name = "carlicenseno")
    private String carLicenseNo;


    /**
     * 司机联系电话
     */
    @Column(name = "tel")
    @ApiModelProperty(value = "司机联系电话", name = "tel")
    private String tel;

    @Column(name = "is_gen_experiment")
    @ApiModelProperty(value = "是否生成试验台账  0-未同步 1-已同步 2-同步失败")
    private Integer isGenExperiment;

    @Column(name = "error_msg")
    @ApiModelProperty(value = "错误信息")
    private String errorMsg;

    @Transient
    @ApiModelProperty(value = "发货人", name = "fhy")
    private String fhy;

    /**
     * 组装试验台账记录
     *
     * @param entity         小票信息
     * @param experimentType 试验类型
     * @return
     */
    public static Experiment payloadExperiment(TglItemorder entity, Integer experimentType) {
        Experiment experiment = new Experiment();
        experiment.tglInit(true);
        experiment.setExperimentType(experimentType);
        experiment.setEntrustTime(new Date());
        experiment.setSampleLevel(entity.getTpz());
        experiment.setExperimentGist("GB 50081-2019《混凝土物理力学性能试验方法标准》、GB/T 50082-2009《普通混凝土长期性能和耐久性能试验方法标准》");
        experiment.setBehalfNumber(Double.valueOf(entity.getFhquantity()));
        experiment.setEntrustReasonCode("CJ");
        experiment.setEntrustReasonName("抽检");

        /**获取对应小票扩展信息*/
        try {
            if (StringUtils.isNotBlank(entity.getExtrainfo())) {
                String entityExtrainfo = StringEscapeUtils.unescapeJava(entity.getExtrainfo());
                JSONObject jsonObject = JSON.parseObject(entityExtrainfo);
                experiment.setEntrustPersonName(jsonObject.getString("fhy"));
            }
        } catch (Exception e) {
            experiment.setEntrustPersonName("");
        }
        return experiment;
    }

    public static void main(String[] args) {

        String str = "{\"fhy\":\"\\u9753\\u4ed4\",\"weightInfo\":{\"startWeight\":15855,\"startWeightTime\":\"2023-12-06 16:15:13\",\"backWeight\":\"\",\"backWeightTime\":\"\",\"startCarWeight\":\"12079\",\"startCarWeightTime\":\"2023-12-06 16:15:48\",\"goQuantity\":1.986845566956064}}";

        JSONObject jsonObject = JSON.parseObject(str);

        String s = StringEscapeUtils.unescapeJava("\\u9753\\u4ed4");
        String s1 = StringEscapeUtils.unescapeJava("连载");
        System.out.println(s);
        System.out.println(s1);
    }
}
